package com.example.NettyOnAndroidServer;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class EchoServer {

    /**
     * 服务端口.
     */
    static int port = 8080;
    /*public EchoServer(int port) {
        this.port = port;
    }*/

    /**
     * 应用程序入口.
     */
    public static void main(String[] args) {
        new EchoServer().run();
    }

    public void run() {
        ServerBootstrap server = new ServerBootstrap();
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            server.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true)
                    .childOption(ChannelOption.TCP_NODELAY, true)
                    .childHandler(new EchoServerInitializer());
            ChannelFuture f = server.bind(port).sync();
            System.out.println("SYSTEM - SERVER PORT: " + port);
            f.channel().closeFuture().sync();

        } catch (InterruptedException e) {
            System.out.println("SYSTEM ERROR: " + e.getMessage());
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }
}